﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

using FITHOU_EDU_Server.Doituong;
using Newtonsoft.Json;

namespace FITHOU_EDU_Server.Account
{
    public partial class MobileLogin : System.Web.UI.Page
    {
        //Global Variable
        String sqlCon = ConfigurationManager.ConnectionStrings["FITHOU_EDU"].ToString();
        string sUserName;
        string sPassword;
        //End Global Variable
        protected void Page_Load(object sender, EventArgs e)
        {
            bValidLogin();
        }

        //->Hàm lấy ra thông tin người dùng theo ID và mật khẩu đã nhập
        private void bValidLogin()
        {
            //-------------
            //string sUserName = Request.Form["sUserName"];
            //string sPassword = Request.Form["sPassword"];
            //-------------
            sUserName = Request.QueryString["sUserName"];
            sPassword = Request.QueryString["sPassword"];
            //-------------
            if(sUserName == null || sPassword == null)
            {
                Response.Write("Thông số kết nối tới serrver không chính xác, vui lòng kiểm tra lại.");
                return;
            }
            //--------------------------------------------
            SqlConnection Con = new SqlConnection(sqlCon);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter = null;
            DataTable tbl = new DataTable();
            //-------------
            try
            {
                Con.Open();
                cmd.CommandText = "sp_ValidLogin";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = Con;
                cmd.Parameters.Add("@PK_sNguoidungID", SqlDbType.VarChar);
                cmd.Parameters.Add("@sMatkhau", SqlDbType.VarChar);
                cmd.Parameters[0].Value = sUserName;
                cmd.Parameters[1].Value = sPassword;
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(tbl);

                //String PK_sNguoidungID;
                //String sHoten;
                //Date dNamsinh;
                //String sDiachi;
                //String sSodienthoai;
                //String sEmail;
                //int iTrangthai;
                //String sMatkhau;

                using (tbl)
                {
                    if (tbl.Rows.Count > 0)
                    {
                        DataRow row = tbl.Rows[0];
                        NguoiDung nguoiDung = new NguoiDung();
                        nguoiDung.PK_sNguoidungID = row["PK_sNguoidungID"].ToString();
                        nguoiDung.sHoten = row["sHoten"].ToString();
                        nguoiDung.dNamsinh = DateTime.Parse(row["dNamsinh"].ToString());
                        nguoiDung.sDiachi = row["sDiachi"].ToString();
                        nguoiDung.sSodienthoai = row["sSodienthoai"].ToString();
                        nguoiDung.sEmail = row["sEmail"].ToString();
                        nguoiDung.iTrangthai = int.Parse(row["iTrangthai"].ToString());
                        nguoiDung.sMatkhau = row["sMatkhau"].ToString();

                        string jsonNguoidung = JsonConvert.SerializeObject(nguoiDung);
                        Response.Write(jsonNguoidung);
                    }
                    else
                    {
                        Response.Write("false");
                    }
                }
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
            }
            finally
            {
                if (Con.State != ConnectionState.Closed)
                    Con.Close();
                adapter.Dispose();
                Con.Dispose();
            }
        }//Hàm lấy ra thông tin người dùng theo ID và mật khẩu đã nhập<-
    }
}